package Visitor;

import Arbori.Nod;
import FunctiiElementare.*;
import FunctiiTrigo.*;
import Operatori.*;

public class CalculVisitor extends Visitor {
	protected double calcul;
	protected String rezultat;

	public CalculVisitor() {
		this.rezultat = "";
		this.calcul = 0;
	}

	public CalculVisitor(double calcul) {
		this.calcul = calcul;
		this.rezultat = Double.toString(calcul);
	}

	public double getCalcul() {
		return calcul;
	}

	public String getRezultat() {
		return rezultat;
	}

	public void setCalcul(double calcul) {
		this.calcul = calcul;
	}

	public void setRezultat(String rezultat) {
		this.rezultat = rezultat;
	}

	public void visit(Cat c) {
		if ((c.getFStang() != null) && (c.getFDrept() != null)) {
			visit(c.getFStang());
			double fs = this.calcul;
			visit(c.getFDrept());
			double fd = this.calcul;
			if (fd != 0)
				this.calcul = fs / fd;
			else
				this.calcul = 0;
		}
	}

	public void visit(Constanta cons) {
		this.calcul = cons.getCt();
	}

	public void visit(Cos c) {
		if (c.getFStang() != null) {
			visit(c.getFStang());
			double fs = this.calcul;
			this.calcul = Math.cos(fs);
		}
	}

	public void visit(Ctg c) {
		if (c.getFStang() != null) {
			visit(c.getFStang());
			double fs = this.calcul;
			this.calcul = (Math.cos(fs)) / (Math.sin(fs));
		}
	}

	public void visit(Log l) {
		if (l.getFStang() != null) {
			visit(l.getFStang());
			double fs = this.calcul;
			this.calcul = Math.log(fs);
		}
	}

	public void visit(Minus m) {
		if ((m.getFStang() != null) && (m.getFDrept() != null)) {
			visit(m.getFStang());
			double fs = this.calcul;
			visit(m.getFDrept());
			double fd = this.calcul;
			this.calcul = fs - fd;
		}
	}

	public void visit(Necunoscuta nec) {
		this.calcul = nec.getX();
	}

	public void visit(Nod n) {
		if (n.toString().contains("Plus"))
			visit((Plus) n);
		else if (n.toString().contains("Constanta"))
			visit((Constanta) n);
		else if (n.toString().contains("Necunoscuta"))
			visit((Necunoscuta) n);
		else if (n.toString().contains("Minus"))
			visit((Minus) n);
		else if (n.toString().contains("Cat"))
			visit((Cat) n);
		else if (n.toString().contains("Produs"))
			visit((Produs) n);
		else if (n.toString().contains("Putere"))
			visit((Putere) n);
		else if (n.toString().contains("Cos"))
			visit((Cos) n);
		else if (n.toString().contains("Sin"))
			visit((Sin) n);
		else if (n.toString().contains("Tg"))
			visit((Tg) n);
		else if (n.toString().contains("Ctg"))
			visit((Ctg) n);
		else if (n.toString().contains("Sqrt"))
			visit((Sqrt) n);
		else if (n.toString().contains("Ln"))
			visit((Ln) n);
		else if (n.toString().contains("Log"))
			visit((Log) n);
		else if (n.toString().contains("Exp"))
			visit((Exp) n);
	}

	public void visit(Plus p) {
		if ((p.getFStang() != null) && (p.getFDrept() != null)) {
			visit(p.getFStang());
			double fs = this.calcul;
			visit(p.getFDrept());
			double fd = this.calcul;
			this.calcul = fs + fd;
		}
	}

	public void visit(Produs p) {
		if ((p.getFStang() != null) && (p.getFDrept() != null)) {
			visit(p.getFStang());
			double fs = this.calcul;
			visit(p.getFDrept());
			double fd = this.calcul;
			this.calcul = fs * fd;
		}
	}

	public void visit(Putere p) {
		if ((p.getFStang() != null) && (p.getFDrept() != null)) {
			visit(p.getFStang());
			double fs = this.calcul;
			visit(p.getFDrept());
			double fd = this.calcul;
			this.calcul = Math.pow(fs, fd);
		}
	}

	public void visit(Sin s) {
		if (s.getFStang() != null) {
			visit(s.getFStang());
			double fs = this.calcul;
			this.calcul = Math.sin(fs);
		}
	}

	public void visit(Tg t) {
		if (t.getFStang() != null) {
			visit(t.getFStang());
			double fs = this.calcul;
			this.calcul = Math.tan(fs);
		}
	}

}
